import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { fallbackSrc } from '@/utils/hotgo';
import { cloneDeep } from 'lodash-es';
import { NImage } from 'naive-ui';
import { h, ref } from 'vue';

const dict = useDictStore();

export class State {
  public id = 0; // ID
  public productId = null; // 商品ID
  public newPrice = 0; // 新人价格
  public status = 1; // 状态：1-上架、2-下架
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间
  public productMainImage = ''; // 商品主图

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export class Points {
  public newMemberPoints = 0; // 积分
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
    productId: [
      { required: true, message: '请选择商品', trigger: 'blur', type: 'number' }
    ],
    newPrice: [
      { required: true, message: '请输入新人价格', trigger: 'blur', type: 'number' },
    ],
    
  };

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'productName',
    component: 'NInput',
    label: '商品名称',
    componentProps: {
      placeholder: '请输入商品名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '商品主图',
    key: 'productMainImage',
    align: 'left',
    width: -1,
      render(row: State) {
        // <FileChooser :maxNumber="10" v-model:value="params.images" fileType="image" />
      return h(NImage, {
        src: row.productMainImage? row.productMainImage[0] : '',
        fallbackSrc: fallbackSrc(),
        
        style: {
          'max-width': '100px',
          'max-height': '100px',
        },
      });;
    },
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: -1,
  },
  {
    title: '新人价格',
    key: 'newPrice',
    align: 'left',
    width: -1,
  },
  {
    title: '销量',
    key: 'sellCount',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable']);
}